package com.superquan.selfstudybootmybatis.utils.saxparse;

import org.springframework.stereotype.Component;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author zwq
 * @description sax解析xml工具类
 * @date 2022/7/12
 */
public class XmlParseUtils {

    /**
     * 解析xml文件
     * @param filePath
     */
   public static void parseXmlBySax(String filePath) throws IOException, SAXException, ParserConfigurationException {
       // 加载文件返回文件的输入流
//       InputStream is = XmlParseUtils.class.getClassLoader().getResourceAsStream(filePath);
       ByteArrayInputStream is = new ByteArrayInputStream(filePath.getBytes("UTF-8"));
       XmlParseHandler handler = new XmlParseHandler();
       // 1. 得到SAX解析工厂
       SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
       // 2. 让工厂生产一个sax解析器
       SAXParser newSAXParser = saxParserFactory.newSAXParser();
       // 3. 传入输入流和handler，解析
       newSAXParser.parse(is, handler);
       is.close();
       handler.getUserInfo();
   }
}
